


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Getting Started Tutorial (Java): First Chapter &mdash; Akka Documentation</title>
    
    <link rel="stylesheet" href="../_static/style.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/prettify.css" type="text/css" />
    <link rel="stylesheet" href="../_static/base.css" type="text/css" />
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Exo:300,400,600,700" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.0.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/toc.js"></script>
    <script type="text/javascript" src="../_static/prettify.js"></script>
    <script type="text/javascript" src="../_static/highlightCode.js"></script>
    <script type="text/javascript" src="../_static/effects.core.js"></script>
    <script type="text/javascript" src="../_static/effects.highlight.js"></script>
    <script type="text/javascript" src="../_static/scrollTo.js"></script>
    <script type="text/javascript" src="../_static/contentsFix.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="top" title="Akka Documentation" href="../index.html" />
    <link rel="up" title="Introduction" href="index.html" />
    <link rel="next" title="Use-case and Deployment Scenarios" href="deployment-scenarios.html" />
    <link rel="prev" title="Getting Started Tutorial (Scala): First Chapter" href="getting-started-first-scala.html" /> 
  </head>
  <body>
  <div class="navbar">
    <div class="navbar-inner">
      <div class="container">
        <div class="navbar-logo">
          <a href="http://akka.io"><img src="../_static/logo-small.png" /></a>
        </div>    
        <ul class="nav">
          <li><a href="http://akka.io/docs">Documentation</a></li>
          <li><a href="http://akka.io/downloads">Download</a></li>
          <li><a href="http://groups.google.com/group/akka-user">Mailing List</a></li>
          <li><a href="http://github.com/akka/akka">Code</a></li>           
          <li><a href="http://typesafe.com/products/typesafe-subscription">Commerical Support</a></li>
        </ul>
      </div>
    </div>
  </div>
  <div class="main">
    <div class="container">
      <div class="page-title">Getting Started Tutorial (Java): First Chapter</div><div class="pdf-link"><a href="http://akka.io/docs/akka/2.0.1/Akka.pdf"><img src="../_static/pdf-icon.png" style="height: 40px;" /></a></div></div>
    <div class="main-container">
      <div class="container">
        <div class="row">
          <div class="span12">
            <ul class="breadcrumb">           
              <li>
                 <span class="divider">|</span> <a href="deployment-scenarios.html">Use-case and Deployment Scenarios</a> <span class="divider">»</span>
              </li>
              <li>
                <a href="../index.html">Contents</a>
              </li>
              <li>
                <span class="divider">«</span> <a href="getting-started-first-scala.html">Getting Started Tutorial (Scala): First Chapter</a> <span class="divider">|</span>
              </li>
              <li>
                Version 2.0.1
              </li>
            </ul>         
          </div>
        </div>
        <div class="row">
          <div class="span9">
            
  <div class="section" id="getting-started-tutorial-java-first-chapter">
<span id="getting-started-first-java"></span><h1>Getting Started Tutorial (Java): First Chapter</h1>
<div class="section" id="introduction">
<h2>Introduction</h2>
<p>Welcome to the first tutorial on how to get started with <a class="reference external" href="http://akka.io">Akka</a> and Java. We assume that you already know what Akka and Java are and will now focus on the steps necessary to start your first project.</p>
<p>There are two variations of this first tutorial:</p>
<ul class="simple">
<li>creating a standalone project and run it from the command line</li>
<li>creating a Maven project and running it from within Maven</li>
</ul>
<p>Since they are so similar we will present them both.</p>
<p>The sample application that we will create is using actors to calculate the value of Pi. Calculating Pi is a CPU intensive operation and we will utilize Akka Actors to write a concurrent solution that scales out to multi-core processors. This sample will be extended in future tutorials to use Akka Remote Actors to scale out on multiple machines in a cluster.</p>
<p>We will be using an algorithm that is called &#8220;embarrassingly parallel&#8221; which just means that each job is completely isolated and not coupled with any other job. Since this algorithm is so parallelizable it suits the actor model very well.</p>
<p>Here is the formula for the algorithm we will use:</p>
<img alt="../_images/pi-formula.png" src="../_images/pi-formula.png" />
<p>In this particular algorithm the master splits the series into chunks which are sent out to each worker actor to be processed. When each worker has processed its chunk it sends a result back to the master which aggregates the total result.</p>
</div>
<div class="section" id="tutorial-source-code">
<h2>Tutorial source code</h2>
<p>If you want don&#8217;t want to type in the code and/or set up a Maven project then you can check out the full tutorial from the Akka GitHub repository. It is in the <tt class="docutils literal"><span class="pre">akka-tutorials/akka-tutorial-first</span></tt> module. You can also browse it online <a class="reference external" href="https://github.com/akka/akka/tree/master/akka-tutorials/akka-tutorial-first">here</a>, with the actual source code <a class="reference external" href="https://github.com/akka/akka/blob/master/akka-tutorials/akka-tutorial-first/src/main/java/akka/tutorial/first/java/Pi.java">here</a>.</p>
<p>To check out the code using Git invoke the following command and you can then you can navigate down to the tutorial.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">git</span> <span class="n">clone</span> <span class="n">git</span><span class="o">://</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">.</span><span class="n">git</span>
<span class="n">$</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorials</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">&gt;</span> <span class="n">git</span> <span class="n">clone</span> <span class="n">git</span><span class="o">://</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">.</span><span class="n">git</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">&gt;</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorials</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span>
</pre></div>
</div>
</div>
<div class="section" id="prerequisites">
<h2>Prerequisites</h2>
<p>This tutorial assumes that you have Java 1.6 or later installed on you machine and <tt class="docutils literal"><span class="pre">java</span></tt> on your <tt class="docutils literal"><span class="pre">PATH</span></tt>. You also need to know how to run commands in a shell (ZSH, Bash, DOS etc.) and a decent text editor or IDE to type in the Java code.</p>
<p>You need to make sure that <tt class="docutils literal"><span class="pre">$JAVA_HOME</span></tt> environment variable is set to the
root of the Java distribution. You also need to make sure that the
<tt class="docutils literal"><span class="pre">$JAVA_HOME/bin</span></tt> is on your <tt class="docutils literal"><span class="pre">PATH</span></tt>.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">export</span> <span class="nc">JAVA_HOME</span><span class="o">=..</span><span class="n">root</span> <span class="n">of</span> <span class="nc">Java</span> <span class="n">distribution</span><span class="o">..</span>
<span class="n">$</span> <span class="n">export</span> <span class="nc">PATH</span><span class="o">=</span><span class="nc">$PATH</span><span class="k">:</span><span class="kt">$JAVA_HOME/bin</span>
</pre></div>
</div>
<p>You can test your installation by invoking <tt class="docutils literal"><span class="pre">java</span></tt>:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">java</span> <span class="o">-</span><span class="n">version</span>
<span class="n">java</span> <span class="n">version</span> <span class="s">&quot;1.6.0_24&quot;</span>
<span class="nc">Java</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="nc">SE</span> <span class="nc">Runtime</span> <span class="nc">Environment</span> <span class="o">(</span><span class="n">build</span> <span class="mf">1.6</span><span class="o">.</span><span class="mi">0</span><span class="n">_24</span><span class="o">-</span><span class="n">b07</span><span class="o">-</span><span class="mi">334</span><span class="o">-</span><span class="mi">10</span><span class="n">M3326</span><span class="o">)</span>
<span class="nc">Java</span> <span class="nc">HotSpot</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="mi">64</span><span class="o">-</span><span class="nc">Bit</span> <span class="nc">Server</span> <span class="nc">VM</span> <span class="o">(</span><span class="n">build</span> <span class="mf">19.1</span><span class="o">-</span><span class="n">b02</span><span class="o">-</span><span class="mi">334</span><span class="o">,</span> <span class="n">mixed</span> <span class="n">mode</span><span class="o">)</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">set</span> <span class="nc">JAVA_HOME</span><span class="o">=..</span><span class="n">root</span> <span class="n">of</span> <span class="nc">Java</span> <span class="n">distribution</span><span class="o">..</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">set</span> <span class="nc">PATH</span><span class="o">=%</span><span class="nc">PATH</span><span class="o">%;%</span><span class="nc">JAVA_HOME</span><span class="o">%/</span><span class="n">bin</span>
</pre></div>
</div>
<p>You can test your installation by invoking <tt class="docutils literal"><span class="pre">java</span></tt>:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">java</span> <span class="o">-</span><span class="n">version</span>
<span class="n">java</span> <span class="n">version</span> <span class="s">&quot;1.6.0_24&quot;</span>
<span class="nc">Java</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="nc">SE</span> <span class="nc">Runtime</span> <span class="nc">Environment</span> <span class="o">(</span><span class="n">build</span> <span class="mf">1.6</span><span class="o">.</span><span class="mi">0</span><span class="n">_24</span><span class="o">-</span><span class="n">b07</span><span class="o">-</span><span class="mi">334</span><span class="o">-</span><span class="mi">10</span><span class="n">M3326</span><span class="o">)</span>
<span class="nc">Java</span> <span class="nc">HotSpot</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="mi">64</span><span class="o">-</span><span class="nc">Bit</span> <span class="nc">Server</span> <span class="nc">VM</span> <span class="o">(</span><span class="n">build</span> <span class="mf">19.1</span><span class="o">-</span><span class="n">b02</span><span class="o">-</span><span class="mi">334</span><span class="o">,</span> <span class="n">mixed</span> <span class="n">mode</span><span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="downloading-and-installing-akka">
<h2>Downloading and installing Akka</h2>
<p>To build and run the tutorial sample from the command line, you have to download
Akka. If you prefer to use SBT to build and run the sample then you can skip this
section and jump to the next one.</p>
<p>Let&#8217;s get the <tt class="docutils literal"><span class="pre">akka-2.0.1.zip</span></tt> distribution of Akka from
<a class="reference external" href="http://akka.io/downloads/">http://akka.io/downloads/</a> which includes everything we need for this
tutorial. Once you have downloaded the distribution unzip it in the folder you
would like to have Akka installed in. In my case I choose to install it in
<tt class="docutils literal"><span class="pre">/Users/jboner/tools/</span></tt>, simply by unzipping it to this directory.</p>
<p>You need to do one more thing in order to install Akka properly: set the
<tt class="docutils literal"><span class="pre">AKKA_HOME</span></tt> environment variable to the root of the distribution. In my case
I&#8217;m opening up a shell, navigating down to the distribution, and setting the
<tt class="docutils literal"><span class="pre">AKKA_HOME</span></tt> variable.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">cd</span> <span class="o">/</span><span class="nc">Users</span><span class="o">/</span><span class="n">jboner</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
<span class="n">$</span> <span class="n">export</span> <span class="nc">AKKA_HOME</span><span class="o">=</span><span class="n">`pwd`</span>
<span class="n">$</span> <span class="n">echo</span> <span class="nc">$AKKA_HOME</span>
<span class="o">/</span><span class="nc">Users</span><span class="o">/</span><span class="n">jboner</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">set</span> <span class="nc">AKKA_HOME</span><span class="o">=%</span><span class="n">cd</span><span class="o">%</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">echo</span> <span class="o">%</span><span class="nc">AKKA_HOME</span><span class="o">%</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
</pre></div>
</div>
<p>The distribution looks like this.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">ls</span> <span class="o">-</span><span class="mi">1</span>
<span class="n">bin</span>
<span class="n">config</span>
<span class="n">deploy</span>
<span class="n">doc</span>
<span class="n">lib</span>
<span class="n">src</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">dir</span>
<span class="n">bin</span>
<span class="n">config</span>
<span class="n">deploy</span>
<span class="n">doc</span>
<span class="n">lib</span>
<span class="n">src</span>
</pre></div>
</div>
<ul class="simple">
<li>In the <tt class="docutils literal"><span class="pre">bin</span></tt> directory we have scripts for starting the Akka Microkernel.</li>
<li>In the <tt class="docutils literal"><span class="pre">config</span></tt> directory we have the Akka conf files.</li>
<li>In the <tt class="docutils literal"><span class="pre">deploy</span></tt> directory we can place applications to be run with the microkernel.</li>
<li>In the <tt class="docutils literal"><span class="pre">doc</span></tt> directory we have the documentation, API, and doc JARs.</li>
<li>In the <tt class="docutils literal"><span class="pre">lib</span></tt> directory we have the Scala and Akka JARs.</li>
<li>In the <tt class="docutils literal"><span class="pre">src</span></tt> directory we have the source JARs for Akka.</li>
</ul>
<p>The only JAR we will need for this tutorial (apart from the
<tt class="docutils literal"><span class="pre">scala-library.jar</span></tt> JAR) is the <tt class="docutils literal"><span class="pre">akka-actor-2.0.1.jar</span></tt> JAR in the <tt class="docutils literal"><span class="pre">lib/akka</span></tt>
directory. This is a self-contained JAR with zero dependencies and contains
everything we need to write a system using Actors.</p>
<p>Akka is very modular and has many JARs for containing different features. The
modules are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">akka-actor</span></tt> &#8211; Actors</li>
<li><tt class="docutils literal"><span class="pre">akka-remote</span></tt> &#8211; Remote Actors</li>
<li><tt class="docutils literal"><span class="pre">akka-slf4j</span></tt> &#8211; SLF4J Event Handler Listener for logging with SLF4J</li>
<li><tt class="docutils literal"><span class="pre">akka-testkit</span></tt> &#8211; Toolkit for testing Actors</li>
<li><tt class="docutils literal"><span class="pre">akka-kernel</span></tt> &#8211; Akka microkernel for running a bare-bones mini application server</li>
<li><tt class="docutils literal"><span class="pre">akka-durable-mailboxes</span></tt> &#8211; Durable mailboxes: file-based, MongoDB, Redis, Beanstalk and Zookeeper</li>
</ul>
</div>
<div class="section" id="downloading-and-installing-maven">
<h2>Downloading and installing Maven</h2>
<p>Maven is an excellent build system that can be used to build both Java and Scala projects. If you want to use Maven for this tutorial then follow the following instructions, if not you can skip this section and the next.</p>
<p>First browse to <a class="reference external" href="http://maven.apache.org/download.html">http://maven.apache.org/download.html</a> and download the <tt class="docutils literal"><span class="pre">3.0.3</span></tt> distribution.</p>
<p>To install Maven it is easiest to follow the instructions on <a class="reference external" href="http://maven.apache.org/download.html#Installation">http://maven.apache.org/download.html#Installation</a>.</p>
</div>
<div class="section" id="creating-an-akka-maven-project">
<h2>Creating an Akka Maven project</h2>
<p>If you have not already done so, now is the time to create a Maven project for our tutorial. You do that by stepping into the directory you want to create your project in and invoking the <tt class="docutils literal"><span class="pre">mvn</span></tt> command.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">mvn</span> <span class="n">archetype</span><span class="k">:</span><span class="kt">generate</span> <span class="kt">\</span>
    <span class="o">-</span><span class="nc">DgroupId</span><span class="o">=</span><span class="n">akka</span><span class="o">.</span><span class="n">tutorial</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">java</span> <span class="o">\</span>
    <span class="o">-</span><span class="nc">DartifactId</span><span class="o">=</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span><span class="o">-</span><span class="n">java</span> <span class="o">\</span>
    <span class="o">-</span><span class="nc">DarchetypeArtifactId</span><span class="o">=</span><span class="n">maven</span><span class="o">-</span><span class="n">archetype</span><span class="o">-</span><span class="n">quickstart</span> <span class="o">\</span>
    <span class="o">-</span><span class="nc">DinteractiveMode</span><span class="o">=</span><span class="kc">false</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">mvn</span> <span class="n">archetype</span><span class="k">:</span><span class="kt">generate</span> <span class="kt">\</span>
    <span class="o">-</span><span class="nc">DgroupId</span><span class="o">=</span><span class="n">akka</span><span class="o">.</span><span class="n">tutorial</span><span class="o">.</span><span class="n">first</span><span class="o">.</span><span class="n">java</span> <span class="o">\</span>
    <span class="o">-</span><span class="nc">DartifactId</span><span class="o">=</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span><span class="o">-</span><span class="n">java</span> <span class="o">\</span>
    <span class="o">-</span><span class="nc">DarchetypeArtifactId</span><span class="o">=</span><span class="n">maven</span><span class="o">-</span><span class="n">archetype</span><span class="o">-</span><span class="n">quickstart</span> <span class="o">\</span>
    <span class="o">-</span><span class="nc">DinteractiveMode</span><span class="o">=</span><span class="kc">false</span>
</pre></div>
</div>
<p>Now we have the basis for our Maven-based Akka project. Let&#8217;s step into the project directory.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span><span class="o">-</span><span class="n">java</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span><span class="o">-</span><span class="n">java</span>
</pre></div>
</div>
<p>Here is the layout that Maven created:</p>
<div class="highlight-scala"><pre>akka-tutorial-first-jboner
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |       `-- akka
    |           `-- tutorial
    |               `-- first
    |                   `-- java
    |                       `-- App.java</pre>
</div>
<p>As you can see we already have a Java source file called <tt class="docutils literal"><span class="pre">App.java</span></tt>, let&#8217;s now rename it to <tt class="docutils literal"><span class="pre">Pi.java</span></tt>.</p>
<p>We also need to edit the <tt class="docutils literal"><span class="pre">pom.xml</span></tt> build file. Let&#8217;s add the dependency we
need as well as the Maven repository it should download it from. The Akka Maven
repository can be found at <a class="reference external" href="http://akka.io/releases/">http://akka.io/releases/</a> and Typesafe provides
<a class="reference external" href="http://repo.typesafe.com/typesafe/releases/">http://repo.typesafe.com/typesafe/releases/</a> that proxies several other
repositories, including akka.io.  It should now look something like this:</p>
<div class="highlight-xml"><div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="nt">&lt;project</span> <span class="na">xmlns=</span><span class="s">&quot;http://maven.apache.org/POM/4.0.0&quot;</span>
         <span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
         <span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://maven.apache.org/POM/4.0.0</span>
<span class="s">         http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;</span><span class="nt">&gt;</span>
    <span class="nt">&lt;modelVersion&gt;</span>4.0.0<span class="nt">&lt;/modelVersion&gt;</span>

    <span class="nt">&lt;name&gt;</span>akka-tutorial-first-java<span class="nt">&lt;/name&gt;</span>
    <span class="nt">&lt;groupId&gt;</span>akka.tutorial.first.java<span class="nt">&lt;/groupId&gt;</span>
    <span class="nt">&lt;artifactId&gt;</span>akka-tutorial-first-java<span class="nt">&lt;/artifactId&gt;</span>
    <span class="nt">&lt;packaging&gt;</span>jar<span class="nt">&lt;/packaging&gt;</span>
    <span class="nt">&lt;version&gt;</span>1.0-SNAPSHOT<span class="nt">&lt;/version&gt;</span>
    <span class="nt">&lt;url&gt;</span>http://akka.io<span class="nt">&lt;/url&gt;</span>

    <span class="nt">&lt;dependencies&gt;</span>
        <span class="nt">&lt;dependency&gt;</span>
            <span class="nt">&lt;groupId&gt;</span>com.typesafe.akka<span class="nt">&lt;/groupId&gt;</span>
            <span class="nt">&lt;artifactId&gt;</span>akka-actor<span class="nt">&lt;/artifactId&gt;</span>
            <span class="nt">&lt;version&gt;</span>2.0.1<span class="nt">&lt;/version&gt;</span>
        <span class="nt">&lt;/dependency&gt;</span>
    <span class="nt">&lt;/dependencies&gt;</span>

    <span class="nt">&lt;repositories&gt;</span>
        <span class="nt">&lt;repository&gt;</span>
            <span class="nt">&lt;id&gt;</span>typesafe<span class="nt">&lt;/id&gt;</span>
            <span class="nt">&lt;name&gt;</span>Typesafe Repository<span class="nt">&lt;/name&gt;</span>
            <span class="nt">&lt;url&gt;</span>http://repo.typesafe.com/typesafe/releases/<span class="nt">&lt;/url&gt;</span>
        <span class="nt">&lt;/repository&gt;</span>
    <span class="nt">&lt;/repositories&gt;</span>

    <span class="nt">&lt;build&gt;</span>
        <span class="nt">&lt;plugins&gt;</span>
            <span class="nt">&lt;plugin&gt;</span>
                <span class="nt">&lt;groupId&gt;</span>org.apache.maven.plugins<span class="nt">&lt;/groupId&gt;</span>
                <span class="nt">&lt;artifactId&gt;</span>maven-compiler-plugin<span class="nt">&lt;/artifactId&gt;</span>
                <span class="nt">&lt;version&gt;</span>2.3.2<span class="nt">&lt;/version&gt;</span>
                <span class="nt">&lt;configuration&gt;</span>
                    <span class="nt">&lt;source&gt;</span>1.6<span class="nt">&lt;/source&gt;</span>
                    <span class="nt">&lt;target&gt;</span>1.6<span class="nt">&lt;/target&gt;</span>
                <span class="nt">&lt;/configuration&gt;</span>
            <span class="nt">&lt;/plugin&gt;</span>
        <span class="nt">&lt;/plugins&gt;</span>
    <span class="nt">&lt;/build&gt;</span>
<span class="nt">&lt;/project&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="start-writing-the-code">
<h2>Start writing the code</h2>
<p>Now it&#8217;s about time to start hacking.</p>
<p>We start by creating a <tt class="docutils literal"><span class="pre">Pi.java</span></tt> file and adding these import statements at the top of the file:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">import</span> <span class="nn">akka.actor.ActorRef</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.ActorSystem</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.Props</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.UntypedActor</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.UntypedActorFactory</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.routing.RoundRobinRouter</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.util.Duration</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">java.util.concurrent.TimeUnit</span><span class="o">;</span>
</pre></div>
</div>
<p>If you are using Maven in this tutorial then create the file in the <tt class="docutils literal"><span class="pre">src/main/java/akka/tutorial/first/java</span></tt> directory.</p>
<p>If you are using the command line tools then create the file wherever you want.
We will create it in a directory called <tt class="docutils literal"><span class="pre">tutorial</span></tt> at the root of the Akka distribution,
e.g. in <tt class="docutils literal"><span class="pre">$AKKA_HOME/tutorial/akka/tutorial/first/java/Pi.java</span></tt>.</p>
</div>
<div class="section" id="creating-the-messages">
<h2>Creating the messages</h2>
<p>The design we are aiming for is to have one <tt class="docutils literal"><span class="pre">Master</span></tt> actor initiating the
computation, creating a set of <tt class="docutils literal"><span class="pre">Worker</span></tt> actors. Then it splits up the work
into discrete chunks, and sends these chunks to the different workers in a
round-robin fashion. The master waits until all the workers have completed their
work and sent back results for aggregation. When computation is completed the
master sends the result to the <tt class="docutils literal"><span class="pre">Listener</span></tt>, which prints out the result.</p>
<p>With this in mind, let&#8217;s now create the messages that we want to have flowing in
the system. We need four different messages:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Calculate</span></tt> &#8211; sent to the <tt class="docutils literal"><span class="pre">Master</span></tt> actor to start the calculation</li>
<li><tt class="docutils literal"><span class="pre">Work</span></tt> &#8211; sent from the <tt class="docutils literal"><span class="pre">Master</span></tt> actor to the <tt class="docutils literal"><span class="pre">Worker</span></tt> actors containing
the work assignment</li>
<li><tt class="docutils literal"><span class="pre">Result</span></tt> &#8211; sent from the <tt class="docutils literal"><span class="pre">Worker</span></tt> actors to the <tt class="docutils literal"><span class="pre">Master</span></tt> actor
containing the result from the worker&#8217;s calculation</li>
<li><tt class="docutils literal"><span class="pre">PiApproximation</span></tt> &#8211; sent from the <tt class="docutils literal"><span class="pre">Master</span></tt> actor to the
<tt class="docutils literal"><span class="pre">Listener</span></tt> actor containing the the final pi result and how long time
the calculation took</li>
</ul>
<p>Messages sent to actors should always be immutable to avoid sharing mutable state. So let&#8217;s start by creating three messages as immutable POJOs. We also create a wrapper <tt class="docutils literal"><span class="pre">Pi</span></tt> class to hold our implementation:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">static</span> <span class="k">class</span> <span class="nc">Calculate</span> <span class="o">{</span>
<span class="o">}</span>

<span class="n">static</span> <span class="k">class</span> <span class="nc">Work</span> <span class="o">{</span>
  <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">start</span><span class="o">;</span>
  <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">;</span>

  <span class="n">public</span> <span class="nc">Work</span><span class="o">(</span><span class="n">int</span> <span class="n">start</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">this</span><span class="o">.</span><span class="n">start</span> <span class="k">=</span> <span class="n">start</span><span class="o">;</span>
    <span class="k">this</span><span class="o">.</span><span class="n">nrOfElements</span> <span class="k">=</span> <span class="n">nrOfElements</span><span class="o">;</span>
  <span class="o">}</span>

  <span class="n">public</span> <span class="n">int</span> <span class="n">getStart</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">return</span> <span class="n">start</span><span class="o">;</span>
  <span class="o">}</span>

  <span class="n">public</span> <span class="n">int</span> <span class="n">getNrOfElements</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">return</span> <span class="n">nrOfElements</span><span class="o">;</span>
  <span class="o">}</span>
<span class="o">}</span>

<span class="n">static</span> <span class="k">class</span> <span class="nc">Result</span> <span class="o">{</span>
  <span class="k">private</span> <span class="k">final</span> <span class="n">double</span> <span class="n">value</span><span class="o">;</span>

  <span class="n">public</span> <span class="nc">Result</span><span class="o">(</span><span class="n">double</span> <span class="n">value</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">this</span><span class="o">.</span><span class="n">value</span> <span class="k">=</span> <span class="n">value</span><span class="o">;</span>
  <span class="o">}</span>

  <span class="n">public</span> <span class="n">double</span> <span class="n">getValue</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">return</span> <span class="n">value</span><span class="o">;</span>
  <span class="o">}</span>
<span class="o">}</span>

<span class="n">static</span> <span class="k">class</span> <span class="nc">PiApproximation</span> <span class="o">{</span>
  <span class="k">private</span> <span class="k">final</span> <span class="n">double</span> <span class="n">pi</span><span class="o">;</span>
  <span class="k">private</span> <span class="k">final</span> <span class="nc">Duration</span> <span class="n">duration</span><span class="o">;</span>

  <span class="n">public</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">double</span> <span class="n">pi</span><span class="o">,</span> <span class="nc">Duration</span> <span class="n">duration</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">this</span><span class="o">.</span><span class="n">pi</span> <span class="k">=</span> <span class="n">pi</span><span class="o">;</span>
    <span class="k">this</span><span class="o">.</span><span class="n">duration</span> <span class="k">=</span> <span class="n">duration</span><span class="o">;</span>
  <span class="o">}</span>

  <span class="n">public</span> <span class="n">double</span> <span class="n">getPi</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">return</span> <span class="n">pi</span><span class="o">;</span>
  <span class="o">}</span>

  <span class="n">public</span> <span class="nc">Duration</span> <span class="n">getDuration</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">return</span> <span class="n">duration</span><span class="o">;</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-the-worker">
<h2>Creating the worker</h2>
<p>Now we can create the worker actor.  This is done by extending in the <tt class="docutils literal"><span class="pre">UntypedActor</span></tt> base class and defining the <tt class="docutils literal"><span class="pre">onReceive</span></tt> method. The <tt class="docutils literal"><span class="pre">onReceive</span></tt> method defines our message handler. We expect it to be able to handle the <tt class="docutils literal"><span class="pre">Work</span></tt> message so we need to add a handler for this message:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">public</span> <span class="n">static</span> <span class="k">class</span> <span class="nc">Worker</span> <span class="k">extends</span> <span class="nc">UntypedActor</span> <span class="o">{</span>

  <span class="c1">// calculatePiFor ...</span>

  <span class="n">public</span> <span class="n">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="nc">Object</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">Work</span><span class="o">)</span> <span class="o">{</span>
      <span class="nc">Work</span> <span class="n">work</span> <span class="k">=</span> <span class="o">(</span><span class="nc">Work</span><span class="o">)</span> <span class="n">message</span><span class="o">;</span>
      <span class="n">double</span> <span class="n">result</span> <span class="k">=</span> <span class="n">calculatePiFor</span><span class="o">(</span><span class="n">work</span><span class="o">.</span><span class="n">getStart</span><span class="o">(),</span> <span class="n">work</span><span class="o">.</span><span class="n">getNrOfElements</span><span class="o">());</span>
      <span class="n">getSender</span><span class="o">().</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">Result</span><span class="o">(</span><span class="n">result</span><span class="o">),</span> <span class="n">getSelf</span><span class="o">());</span>
    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
      <span class="n">unhandled</span><span class="o">(</span><span class="n">message</span><span class="o">);</span>
    <span class="o">}</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>As you can see we have now created an <tt class="docutils literal"><span class="pre">UntypedActor</span></tt> with a <tt class="docutils literal"><span class="pre">onReceive</span></tt> method as a handler for the <tt class="docutils literal"><span class="pre">Work</span></tt> message. In this handler we invoke the <tt class="docutils literal"><span class="pre">calculatePiFor(..)</span></tt> method, wrap the result in a <tt class="docutils literal"><span class="pre">Result</span></tt> message and send it back to the original sender using <tt class="docutils literal"><span class="pre">getContext().reply(..)</span></tt>. In Akka the sender reference is implicitly passed along with the message so that the receiver can always reply or store away the sender reference for future use.</p>
<p>The only thing missing in our <tt class="docutils literal"><span class="pre">Worker</span></tt> actor is the implementation on the <tt class="docutils literal"><span class="pre">calculatePiFor(..)</span></tt> method:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">private</span> <span class="n">double</span> <span class="n">calculatePiFor</span><span class="o">(</span><span class="n">int</span> <span class="n">start</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">)</span> <span class="o">{</span>
  <span class="n">double</span> <span class="n">acc</span> <span class="k">=</span> <span class="mf">0.0</span><span class="o">;</span>
  <span class="k">for</span> <span class="o">(</span><span class="n">int</span> <span class="n">i</span> <span class="k">=</span> <span class="n">start</span> <span class="o">*</span> <span class="n">nrOfElements</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="o">((</span><span class="n">start</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">*</span> <span class="n">nrOfElements</span> <span class="o">-</span> <span class="mi">1</span><span class="o">);</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
    <span class="n">acc</span> <span class="o">+=</span> <span class="mf">4.0</span> <span class="o">*</span> <span class="o">(</span><span class="mi">1</span> <span class="o">-</span> <span class="o">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="o">)</span> <span class="o">*</span> <span class="mi">2</span><span class="o">)</span> <span class="o">/</span> <span class="o">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="o">);</span>
  <span class="o">}</span>
  <span class="k">return</span> <span class="n">acc</span><span class="o">;</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-the-master">
<h2>Creating the master</h2>
<p>The master actor is a little bit more involved. In its constructor we create a round-robin router
to make it easier to spread out the work evenly between the workers. Let&#8217;s do that first:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">workerRouter</span> <span class="k">=</span> <span class="k">this</span><span class="o">.</span><span class="n">getContext</span><span class="o">().</span><span class="n">actorOf</span><span class="o">(</span><span class="k">new</span> <span class="nc">Props</span><span class="o">(</span><span class="nc">Worker</span><span class="o">.</span><span class="n">class</span><span class="o">).</span><span class="n">withRouter</span><span class="o">(</span><span class="k">new</span> <span class="nc">RoundRobinRouter</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">)),</span>
    <span class="s">&quot;workerRouter&quot;</span><span class="o">);</span>
</pre></div>
</div>
<p>Now we have a router that is representing all our workers in a single
abstraction. So now let&#8217;s create the master actor. We pass it three integer variables:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">nrOfWorkers</span></tt> &#8211; defining how many workers we should start up</li>
<li><tt class="docutils literal"><span class="pre">nrOfMessages</span></tt> &#8211; defining how many number chunks to send out to the workers</li>
<li><tt class="docutils literal"><span class="pre">nrOfElements</span></tt> &#8211; defining how big the number chunks sent to each worker should be</li>
</ul>
<p>Here is the master actor:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">public</span> <span class="n">static</span> <span class="k">class</span> <span class="nc">Master</span> <span class="k">extends</span> <span class="nc">UntypedActor</span> <span class="o">{</span>
  <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfMessages</span><span class="o">;</span>
  <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">;</span>

  <span class="k">private</span> <span class="n">double</span> <span class="n">pi</span><span class="o">;</span>
  <span class="k">private</span> <span class="n">int</span> <span class="n">nrOfResults</span><span class="o">;</span>
  <span class="k">private</span> <span class="k">final</span> <span class="n">long</span> <span class="n">start</span> <span class="k">=</span> <span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span><span class="o">();</span>

  <span class="k">private</span> <span class="k">final</span> <span class="nc">ActorRef</span> <span class="n">listener</span><span class="o">;</span>
  <span class="k">private</span> <span class="k">final</span> <span class="nc">ActorRef</span> <span class="n">workerRouter</span><span class="o">;</span>

  <span class="n">public</span> <span class="nc">Master</span><span class="o">(</span><span class="k">final</span> <span class="n">int</span> <span class="n">nrOfWorkers</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfMessages</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="nc">ActorRef</span> <span class="n">listener</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">this</span><span class="o">.</span><span class="n">nrOfMessages</span> <span class="k">=</span> <span class="n">nrOfMessages</span><span class="o">;</span>
    <span class="k">this</span><span class="o">.</span><span class="n">nrOfElements</span> <span class="k">=</span> <span class="n">nrOfElements</span><span class="o">;</span>
    <span class="k">this</span><span class="o">.</span><span class="n">listener</span> <span class="k">=</span> <span class="n">listener</span><span class="o">;</span>

    <span class="n">workerRouter</span> <span class="k">=</span> <span class="k">this</span><span class="o">.</span><span class="n">getContext</span><span class="o">().</span><span class="n">actorOf</span><span class="o">(</span><span class="k">new</span> <span class="nc">Props</span><span class="o">(</span><span class="nc">Worker</span><span class="o">.</span><span class="n">class</span><span class="o">).</span><span class="n">withRouter</span><span class="o">(</span><span class="k">new</span> <span class="nc">RoundRobinRouter</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">)),</span>
        <span class="s">&quot;workerRouter&quot;</span><span class="o">);</span>
  <span class="o">}</span>

  <span class="n">public</span> <span class="n">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="nc">Object</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
    <span class="c1">// handle messages ...</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>A couple of things are worth explaining further.</p>
<p>Note that we are passing in a <tt class="docutils literal"><span class="pre">ActorRef</span></tt> to the <tt class="docutils literal"><span class="pre">Master</span></tt> actor. This is used to
report the the final result to the outside world.</p>
<p>But we are not done yet. We are missing the message handler for the <tt class="docutils literal"><span class="pre">Master</span></tt> actor.
This message handler needs to be able to react to two different messages:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Calculate</span></tt> &#8211; which should start the calculation</li>
<li><tt class="docutils literal"><span class="pre">Result</span></tt> &#8211; which should aggregate the different results</li>
</ul>
<p>The <tt class="docutils literal"><span class="pre">Calculate</span></tt> handler is sending out work to all the <tt class="docutils literal"><span class="pre">Worker</span></tt> via its router.</p>
<p>The <tt class="docutils literal"><span class="pre">Result</span></tt> handler gets the value from the <tt class="docutils literal"><span class="pre">Result</span></tt> message and aggregates it to
our <tt class="docutils literal"><span class="pre">pi</span></tt> member variable. We also keep track of how many results we have received back,
and if that matches the number of tasks sent out, the <tt class="docutils literal"><span class="pre">Master</span></tt> actor considers itself done and
sends the final result to the <tt class="docutils literal"><span class="pre">listener</span></tt>. When done it also invokes the <tt class="docutils literal"><span class="pre">getContext().stop(getSelf())</span></tt>
method to stop itself <em>and</em> all its supervised actors.
In this case it has one supervised actor, the router, and this in turn has <tt class="docutils literal"><span class="pre">nrOfWorkers</span></tt> supervised actors.
All of them will be stopped automatically as the invocation of any supervisor&#8217;s <tt class="docutils literal"><span class="pre">stop</span></tt> method
will propagate down to all its supervised &#8216;children&#8217;.</p>
<p>Let&#8217;s capture this in code:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">public</span> <span class="n">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="nc">Object</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
  <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">Calculate</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">for</span> <span class="o">(</span><span class="n">int</span> <span class="n">start</span> <span class="k">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">start</span> <span class="o">&lt;</span> <span class="n">nrOfMessages</span><span class="o">;</span> <span class="n">start</span><span class="o">++)</span> <span class="o">{</span>
      <span class="n">workerRouter</span><span class="o">.</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">Work</span><span class="o">(</span><span class="n">start</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">),</span> <span class="n">getSelf</span><span class="o">());</span>
    <span class="o">}</span>
  <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">Result</span><span class="o">)</span> <span class="o">{</span>
    <span class="nc">Result</span> <span class="n">result</span> <span class="k">=</span> <span class="o">(</span><span class="nc">Result</span><span class="o">)</span> <span class="n">message</span><span class="o">;</span>
    <span class="n">pi</span> <span class="o">+=</span> <span class="n">result</span><span class="o">.</span><span class="n">getValue</span><span class="o">();</span>
    <span class="n">nrOfResults</span> <span class="o">+=</span> <span class="mi">1</span><span class="o">;</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">nrOfResults</span> <span class="o">==</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="o">{</span>
      <span class="c1">// Send the result to the listener</span>
      <span class="nc">Duration</span> <span class="n">duration</span> <span class="k">=</span> <span class="nc">Duration</span><span class="o">.</span><span class="n">create</span><span class="o">(</span><span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span><span class="o">()</span> <span class="o">-</span> <span class="n">start</span><span class="o">,</span> <span class="nc">TimeUnit</span><span class="o">.</span><span class="nc">MILLISECONDS</span><span class="o">);</span>
      <span class="n">listener</span><span class="o">.</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span><span class="o">),</span> <span class="n">getSelf</span><span class="o">());</span>
      <span class="c1">// Stops this actor and all its supervised children</span>
      <span class="n">getContext</span><span class="o">().</span><span class="n">stop</span><span class="o">(</span><span class="n">getSelf</span><span class="o">());</span>
    <span class="o">}</span>
  <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
    <span class="n">unhandled</span><span class="o">(</span><span class="n">message</span><span class="o">);</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-the-result-listener">
<h2>Creating the result listener</h2>
<p>The listener is straightforward. When it receives the <tt class="docutils literal"><span class="pre">PiApproximation</span></tt> from the <tt class="docutils literal"><span class="pre">Master</span></tt> it
prints the result and shuts down the <tt class="docutils literal"><span class="pre">ActorSystem</span></tt>.</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">public</span> <span class="n">static</span> <span class="k">class</span> <span class="nc">Listener</span> <span class="k">extends</span> <span class="nc">UntypedActor</span> <span class="o">{</span>
  <span class="n">public</span> <span class="n">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="nc">Object</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">PiApproximation</span><span class="o">)</span> <span class="o">{</span>
      <span class="nc">PiApproximation</span> <span class="n">approximation</span> <span class="k">=</span> <span class="o">(</span><span class="nc">PiApproximation</span><span class="o">)</span> <span class="n">message</span><span class="o">;</span>
      <span class="nc">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">println</span><span class="o">(</span><span class="nc">String</span><span class="o">.</span><span class="n">format</span><span class="o">(</span><span class="s">&quot;\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s&quot;</span><span class="o">,</span>
          <span class="n">approximation</span><span class="o">.</span><span class="n">getPi</span><span class="o">(),</span> <span class="n">approximation</span><span class="o">.</span><span class="n">getDuration</span><span class="o">()));</span>
      <span class="n">getContext</span><span class="o">().</span><span class="n">system</span><span class="o">().</span><span class="n">shutdown</span><span class="o">();</span>
    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
      <span class="n">unhandled</span><span class="o">(</span><span class="n">message</span><span class="o">);</span>
    <span class="o">}</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="bootstrap-the-calculation">
<h2>Bootstrap the calculation</h2>
<p>Now the only thing that is left to implement is the runner that should bootstrap and run the calculation for us.
We do that by adding a <tt class="docutils literal"><span class="pre">main</span></tt> method to the enclosing <tt class="docutils literal"><span class="pre">Pi</span></tt> class in which we create a new instance of <tt class="docutils literal"><span class="pre">Pi</span></tt> and
invoke method <tt class="docutils literal"><span class="pre">calculate</span></tt> in which we start up the <tt class="docutils literal"><span class="pre">Master</span></tt> actor and wait for it to finish:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">public</span> <span class="k">class</span> <span class="nc">Pi</span> <span class="o">{</span>

  <span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
    <span class="nc">Pi</span> <span class="n">pi</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Pi</span><span class="o">();</span>
    <span class="n">pi</span><span class="o">.</span><span class="n">calculate</span><span class="o">(</span><span class="mi">4</span><span class="o">,</span> <span class="mi">10000</span><span class="o">,</span> <span class="mi">10000</span><span class="o">);</span>
  <span class="o">}</span>

  <span class="c1">// actors and messages ...</span>

  <span class="n">public</span> <span class="n">void</span> <span class="n">calculate</span><span class="o">(</span><span class="k">final</span> <span class="n">int</span> <span class="n">nrOfWorkers</span><span class="o">,</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="o">{</span>
    <span class="c1">// Create an Akka system</span>
    <span class="nc">ActorSystem</span> <span class="n">system</span> <span class="k">=</span> <span class="nc">ActorSystem</span><span class="o">.</span><span class="n">create</span><span class="o">(</span><span class="s">&quot;PiSystem&quot;</span><span class="o">);</span>

    <span class="c1">// create the result listener, which will print the result and shutdown the system</span>
    <span class="k">final</span> <span class="nc">ActorRef</span> <span class="n">listener</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="k">new</span> <span class="nc">Props</span><span class="o">(</span><span class="nc">Listener</span><span class="o">.</span><span class="n">class</span><span class="o">),</span> <span class="s">&quot;listener&quot;</span><span class="o">);</span>

    <span class="c1">// create the master</span>
    <span class="nc">ActorRef</span> <span class="n">master</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="k">new</span> <span class="nc">Props</span><span class="o">(</span><span class="k">new</span> <span class="nc">UntypedActorFactory</span><span class="o">()</span> <span class="o">{</span>
      <span class="n">public</span> <span class="nc">UntypedActor</span> <span class="n">create</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="nc">Master</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="n">listener</span><span class="o">);</span>
      <span class="o">}</span>
    <span class="o">}),</span> <span class="s">&quot;master&quot;</span><span class="o">);</span>

    <span class="c1">// start the calculation</span>
    <span class="n">master</span><span class="o">.</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">Calculate</span><span class="o">());</span>

  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>As you can see the <em>calculate</em> method above it creates an <tt class="docutils literal"><span class="pre">ActorSystem</span></tt> and this is the Akka container which
will contain all actors created in that &#8220;context&#8221;. An example of how to create actors in the container
is the <em>&#8216;system.actorOf(...)&#8217;</em> line in the calculate method. In this case we create two top level actors.
If you instead where in an actor context, i.e. inside an actor creating other actors, you should use
<em>getContext().actorOf(...)</em>. This is illustrated in the Master code above.</p>
<p>That&#8217;s it. Now we are done.</p>
<p>Before we package it up and run it, let&#8217;s take a look at the full code now, with package declaration, imports and all:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="cm">/**</span>
<span class="cm"> * Copyright (C) 2009-2012 Typesafe Inc. &lt;http://www.typesafe.com&gt;</span>
<span class="cm"> */</span>

<span class="k">package</span> <span class="nn">akka.tutorial.first.java</span><span class="o">;</span>


<span class="k">import</span> <span class="nn">akka.actor.ActorRef</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.ActorSystem</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.Props</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.UntypedActor</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.actor.UntypedActorFactory</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.routing.RoundRobinRouter</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">akka.util.Duration</span><span class="o">;</span>
<span class="k">import</span> <span class="nn">java.util.concurrent.TimeUnit</span><span class="o">;</span>


<span class="n">public</span> <span class="k">class</span> <span class="nc">Pi</span> <span class="o">{</span>

  <span class="n">public</span> <span class="n">static</span> <span class="n">void</span> <span class="n">main</span><span class="o">(</span><span class="nc">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
    <span class="nc">Pi</span> <span class="n">pi</span> <span class="k">=</span> <span class="k">new</span> <span class="nc">Pi</span><span class="o">();</span>
    <span class="n">pi</span><span class="o">.</span><span class="n">calculate</span><span class="o">(</span><span class="mi">4</span><span class="o">,</span> <span class="mi">10000</span><span class="o">,</span> <span class="mi">10000</span><span class="o">);</span>
  <span class="o">}</span>

  <span class="n">static</span> <span class="k">class</span> <span class="nc">Calculate</span> <span class="o">{</span>
  <span class="o">}</span>

  <span class="n">static</span> <span class="k">class</span> <span class="nc">Work</span> <span class="o">{</span>
    <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">start</span><span class="o">;</span>
    <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">;</span>

    <span class="n">public</span> <span class="nc">Work</span><span class="o">(</span><span class="n">int</span> <span class="n">start</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="n">start</span> <span class="k">=</span> <span class="n">start</span><span class="o">;</span>
      <span class="k">this</span><span class="o">.</span><span class="n">nrOfElements</span> <span class="k">=</span> <span class="n">nrOfElements</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="n">public</span> <span class="n">int</span> <span class="n">getStart</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">start</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="n">public</span> <span class="n">int</span> <span class="n">getNrOfElements</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">nrOfElements</span><span class="o">;</span>
    <span class="o">}</span>
  <span class="o">}</span>

  <span class="n">static</span> <span class="k">class</span> <span class="nc">Result</span> <span class="o">{</span>
    <span class="k">private</span> <span class="k">final</span> <span class="n">double</span> <span class="n">value</span><span class="o">;</span>

    <span class="n">public</span> <span class="nc">Result</span><span class="o">(</span><span class="n">double</span> <span class="n">value</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="n">value</span> <span class="k">=</span> <span class="n">value</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="n">public</span> <span class="n">double</span> <span class="n">getValue</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">value</span><span class="o">;</span>
    <span class="o">}</span>
  <span class="o">}</span>

  <span class="n">static</span> <span class="k">class</span> <span class="nc">PiApproximation</span> <span class="o">{</span>
    <span class="k">private</span> <span class="k">final</span> <span class="n">double</span> <span class="n">pi</span><span class="o">;</span>
    <span class="k">private</span> <span class="k">final</span> <span class="nc">Duration</span> <span class="n">duration</span><span class="o">;</span>

    <span class="n">public</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">double</span> <span class="n">pi</span><span class="o">,</span> <span class="nc">Duration</span> <span class="n">duration</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="n">pi</span> <span class="k">=</span> <span class="n">pi</span><span class="o">;</span>
      <span class="k">this</span><span class="o">.</span><span class="n">duration</span> <span class="k">=</span> <span class="n">duration</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="n">public</span> <span class="n">double</span> <span class="n">getPi</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">pi</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="n">public</span> <span class="nc">Duration</span> <span class="n">getDuration</span><span class="o">()</span> <span class="o">{</span>
      <span class="k">return</span> <span class="n">duration</span><span class="o">;</span>
    <span class="o">}</span>
  <span class="o">}</span>


  <span class="n">public</span> <span class="n">static</span> <span class="k">class</span> <span class="nc">Worker</span> <span class="k">extends</span> <span class="nc">UntypedActor</span> <span class="o">{</span>

    <span class="k">private</span> <span class="n">double</span> <span class="n">calculatePiFor</span><span class="o">(</span><span class="n">int</span> <span class="n">start</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">)</span> <span class="o">{</span>
      <span class="n">double</span> <span class="n">acc</span> <span class="k">=</span> <span class="mf">0.0</span><span class="o">;</span>
      <span class="k">for</span> <span class="o">(</span><span class="n">int</span> <span class="n">i</span> <span class="k">=</span> <span class="n">start</span> <span class="o">*</span> <span class="n">nrOfElements</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="o">((</span><span class="n">start</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span> <span class="o">*</span> <span class="n">nrOfElements</span> <span class="o">-</span> <span class="mi">1</span><span class="o">);</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="mf">4.0</span> <span class="o">*</span> <span class="o">(</span><span class="mi">1</span> <span class="o">-</span> <span class="o">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="o">)</span> <span class="o">*</span> <span class="mi">2</span><span class="o">)</span> <span class="o">/</span> <span class="o">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="o">);</span>
      <span class="o">}</span>
      <span class="k">return</span> <span class="n">acc</span><span class="o">;</span>
    <span class="o">}</span>


    <span class="n">public</span> <span class="n">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="nc">Object</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">Work</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">Work</span> <span class="n">work</span> <span class="k">=</span> <span class="o">(</span><span class="nc">Work</span><span class="o">)</span> <span class="n">message</span><span class="o">;</span>
        <span class="n">double</span> <span class="n">result</span> <span class="k">=</span> <span class="n">calculatePiFor</span><span class="o">(</span><span class="n">work</span><span class="o">.</span><span class="n">getStart</span><span class="o">(),</span> <span class="n">work</span><span class="o">.</span><span class="n">getNrOfElements</span><span class="o">());</span>
        <span class="n">getSender</span><span class="o">().</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">Result</span><span class="o">(</span><span class="n">result</span><span class="o">),</span> <span class="n">getSelf</span><span class="o">());</span>
      <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
        <span class="n">unhandled</span><span class="o">(</span><span class="n">message</span><span class="o">);</span>
      <span class="o">}</span>
    <span class="o">}</span>
  <span class="o">}</span>


  <span class="n">public</span> <span class="n">static</span> <span class="k">class</span> <span class="nc">Master</span> <span class="k">extends</span> <span class="nc">UntypedActor</span> <span class="o">{</span>
    <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfMessages</span><span class="o">;</span>
    <span class="k">private</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">;</span>

    <span class="k">private</span> <span class="n">double</span> <span class="n">pi</span><span class="o">;</span>
    <span class="k">private</span> <span class="n">int</span> <span class="n">nrOfResults</span><span class="o">;</span>
    <span class="k">private</span> <span class="k">final</span> <span class="n">long</span> <span class="n">start</span> <span class="k">=</span> <span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span><span class="o">();</span>

    <span class="k">private</span> <span class="k">final</span> <span class="nc">ActorRef</span> <span class="n">listener</span><span class="o">;</span>
    <span class="k">private</span> <span class="k">final</span> <span class="nc">ActorRef</span> <span class="n">workerRouter</span><span class="o">;</span>

    <span class="n">public</span> <span class="nc">Master</span><span class="o">(</span><span class="k">final</span> <span class="n">int</span> <span class="n">nrOfWorkers</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfMessages</span><span class="o">,</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="nc">ActorRef</span> <span class="n">listener</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">this</span><span class="o">.</span><span class="n">nrOfMessages</span> <span class="k">=</span> <span class="n">nrOfMessages</span><span class="o">;</span>
      <span class="k">this</span><span class="o">.</span><span class="n">nrOfElements</span> <span class="k">=</span> <span class="n">nrOfElements</span><span class="o">;</span>
      <span class="k">this</span><span class="o">.</span><span class="n">listener</span> <span class="k">=</span> <span class="n">listener</span><span class="o">;</span>

      <span class="n">workerRouter</span> <span class="k">=</span> <span class="k">this</span><span class="o">.</span><span class="n">getContext</span><span class="o">().</span><span class="n">actorOf</span><span class="o">(</span><span class="k">new</span> <span class="nc">Props</span><span class="o">(</span><span class="nc">Worker</span><span class="o">.</span><span class="n">class</span><span class="o">).</span><span class="n">withRouter</span><span class="o">(</span><span class="k">new</span> <span class="nc">RoundRobinRouter</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">)),</span>
          <span class="s">&quot;workerRouter&quot;</span><span class="o">);</span>
    <span class="o">}</span>

    <span class="n">public</span> <span class="n">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="nc">Object</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">Calculate</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">for</span> <span class="o">(</span><span class="n">int</span> <span class="n">start</span> <span class="k">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">start</span> <span class="o">&lt;</span> <span class="n">nrOfMessages</span><span class="o">;</span> <span class="n">start</span><span class="o">++)</span> <span class="o">{</span>
          <span class="n">workerRouter</span><span class="o">.</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">Work</span><span class="o">(</span><span class="n">start</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">),</span> <span class="n">getSelf</span><span class="o">());</span>
        <span class="o">}</span>
      <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">Result</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">Result</span> <span class="n">result</span> <span class="k">=</span> <span class="o">(</span><span class="nc">Result</span><span class="o">)</span> <span class="n">message</span><span class="o">;</span>
        <span class="n">pi</span> <span class="o">+=</span> <span class="n">result</span><span class="o">.</span><span class="n">getValue</span><span class="o">();</span>
        <span class="n">nrOfResults</span> <span class="o">+=</span> <span class="mi">1</span><span class="o">;</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">nrOfResults</span> <span class="o">==</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="o">{</span>
          <span class="c1">// Send the result to the listener</span>
          <span class="nc">Duration</span> <span class="n">duration</span> <span class="k">=</span> <span class="nc">Duration</span><span class="o">.</span><span class="n">create</span><span class="o">(</span><span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span><span class="o">()</span> <span class="o">-</span> <span class="n">start</span><span class="o">,</span> <span class="nc">TimeUnit</span><span class="o">.</span><span class="nc">MILLISECONDS</span><span class="o">);</span>
          <span class="n">listener</span><span class="o">.</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span><span class="o">),</span> <span class="n">getSelf</span><span class="o">());</span>
          <span class="c1">// Stops this actor and all its supervised children</span>
          <span class="n">getContext</span><span class="o">().</span><span class="n">stop</span><span class="o">(</span><span class="n">getSelf</span><span class="o">());</span>
        <span class="o">}</span>
      <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
        <span class="n">unhandled</span><span class="o">(</span><span class="n">message</span><span class="o">);</span>
      <span class="o">}</span>
    <span class="o">}</span>
  <span class="o">}</span>


  <span class="n">public</span> <span class="n">static</span> <span class="k">class</span> <span class="nc">Listener</span> <span class="k">extends</span> <span class="nc">UntypedActor</span> <span class="o">{</span>
    <span class="n">public</span> <span class="n">void</span> <span class="n">onReceive</span><span class="o">(</span><span class="nc">Object</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span>
      <span class="k">if</span> <span class="o">(</span><span class="n">message</span> <span class="n">instanceof</span> <span class="nc">PiApproximation</span><span class="o">)</span> <span class="o">{</span>
        <span class="nc">PiApproximation</span> <span class="n">approximation</span> <span class="k">=</span> <span class="o">(</span><span class="nc">PiApproximation</span><span class="o">)</span> <span class="n">message</span><span class="o">;</span>
        <span class="nc">System</span><span class="o">.</span><span class="n">out</span><span class="o">.</span><span class="n">println</span><span class="o">(</span><span class="nc">String</span><span class="o">.</span><span class="n">format</span><span class="o">(</span><span class="s">&quot;\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s&quot;</span><span class="o">,</span>
            <span class="n">approximation</span><span class="o">.</span><span class="n">getPi</span><span class="o">(),</span> <span class="n">approximation</span><span class="o">.</span><span class="n">getDuration</span><span class="o">()));</span>
        <span class="n">getContext</span><span class="o">().</span><span class="n">system</span><span class="o">().</span><span class="n">shutdown</span><span class="o">();</span>
      <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
        <span class="n">unhandled</span><span class="o">(</span><span class="n">message</span><span class="o">);</span>
      <span class="o">}</span>
    <span class="o">}</span>
  <span class="o">}</span>


  <span class="n">public</span> <span class="n">void</span> <span class="n">calculate</span><span class="o">(</span><span class="k">final</span> <span class="n">int</span> <span class="n">nrOfWorkers</span><span class="o">,</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="k">final</span> <span class="n">int</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="o">{</span>
    <span class="c1">// Create an Akka system</span>
    <span class="nc">ActorSystem</span> <span class="n">system</span> <span class="k">=</span> <span class="nc">ActorSystem</span><span class="o">.</span><span class="n">create</span><span class="o">(</span><span class="s">&quot;PiSystem&quot;</span><span class="o">);</span>

    <span class="c1">// create the result listener, which will print the result and shutdown the system</span>
    <span class="k">final</span> <span class="nc">ActorRef</span> <span class="n">listener</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="k">new</span> <span class="nc">Props</span><span class="o">(</span><span class="nc">Listener</span><span class="o">.</span><span class="n">class</span><span class="o">),</span> <span class="s">&quot;listener&quot;</span><span class="o">);</span>

    <span class="c1">// create the master</span>
    <span class="nc">ActorRef</span> <span class="n">master</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="k">new</span> <span class="nc">Props</span><span class="o">(</span><span class="k">new</span> <span class="nc">UntypedActorFactory</span><span class="o">()</span> <span class="o">{</span>
      <span class="n">public</span> <span class="nc">UntypedActor</span> <span class="n">create</span><span class="o">()</span> <span class="o">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="nc">Master</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="n">listener</span><span class="o">);</span>
      <span class="o">}</span>
    <span class="o">}),</span> <span class="s">&quot;master&quot;</span><span class="o">);</span>

    <span class="c1">// start the calculation</span>
    <span class="n">master</span><span class="o">.</span><span class="n">tell</span><span class="o">(</span><span class="k">new</span> <span class="nc">Calculate</span><span class="o">());</span>

  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="run-it-as-a-command-line-application">
<h2>Run it as a command line application</h2>
<p>If you have not typed in (or copied) the code for the tutorial as
<tt class="docutils literal"><span class="pre">$AKKA_HOME/tutorial/akka/tutorial/first/java/Pi.java</span></tt> then now is the
time. When that&#8217;s done open up a shell and step in to the Akka distribution
(<tt class="docutils literal"><span class="pre">cd</span> <span class="pre">$AKKA_HOME</span></tt>).</p>
<p>First we need to compile the source file. That is done with Java&#8217;s compiler
<tt class="docutils literal"><span class="pre">javac</span></tt>. Our application depends on the <tt class="docutils literal"><span class="pre">akka-actor-2.0.1.jar</span></tt> and the
<tt class="docutils literal"><span class="pre">scala-library.jar</span></tt> JAR files, so let&#8217;s add them to the compiler classpath
when we compile the source.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><pre>$ javac -cp lib/scala-library.jar:lib/akka/akka-actor-2.0.1.jar tutorial/akka/tutorial/first/java/Pi.java</pre>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">javac</span> <span class="o">-</span><span class="n">cp</span> <span class="o">\</span>
   <span class="n">lib</span><span class="o">/</span><span class="n">scala</span><span class="o">-</span><span class="n">library</span><span class="o">.</span><span class="n">jar</span><span class="o">;</span><span class="n">lib</span><span class="o">/</span><span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="n">actor</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mf">1.</span><span class="n">jar</span> <span class="o">\</span>
   <span class="n">tutorial</span><span class="o">/</span><span class="n">akka</span><span class="o">/</span><span class="n">tutorial</span><span class="o">/</span><span class="n">first</span><span class="o">/</span><span class="n">java</span><span class="o">/</span><span class="nc">Pi</span><span class="o">.</span><span class="n">java</span>
</pre></div>
</div>
<p>When we have compiled the source file we are ready to run the application. This
is done with <tt class="docutils literal"><span class="pre">java</span></tt> but yet again we need to add the <tt class="docutils literal"><span class="pre">akka-actor-2.0.1.jar</span></tt>
and the <tt class="docutils literal"><span class="pre">scala-library.jar</span></tt> JAR files to the classpath as well as the classes
we compiled ourselves.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><pre>$ java \
    -cp lib/scala-library.jar:lib/akka/akka-actor-2.0.1.jar:. \
    akka.tutorial.first.scala.Pi

Pi approximation:   3.1415926435897883
Calculation time:   359 millis</pre>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><pre>C:\Users\jboner\src\akka\akka-2.0.1&gt; java \
    -cp lib/scala-library.jar;lib\akka\akka-actor-2.0.1.jar;. \
    akka.tutorial.first.scala.Pi

Pi approximation:   3.1415926435897883
Calculation time:   359 millis</pre>
</div>
<p>Yippee! It is working.</p>
</div>
<div class="section" id="run-it-inside-maven">
<h2>Run it inside Maven</h2>
<p>If you used Maven, then you can run the application directly inside Maven. First you need to compile the project.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">mvn</span> <span class="n">compile</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">mvn</span> <span class="n">compile</span>
</pre></div>
</div>
<p>When this in done we can run our application directly inside Maven.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><pre>$ mvn exec:java -Dexec.mainClass="akka.tutorial.first.java.Pi"
...
Pi approximation:   3.1415926435897883
Calculation time:   359 millis</pre>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><pre>C:\Users\jboner\src\akka\akka-2.0.1&gt; mvn exec:java \
    -Dexec.mainClass="akka.tutorial.first.java.Pi"
...
Pi approximation:   3.1415926435897883
Calculation time:   359 millis</pre>
</div>
<p>Yippee! It is working.</p>
</div>
<div class="section" id="overriding-configuration-externally-optional">
<h2>Overriding Configuration Externally (Optional)</h2>
<p>The sample project includes an <tt class="docutils literal"><span class="pre">application.conf</span></tt> file in the resources directory:</p>
<div class="highlight-scala"><pre>akka.actor.deployment {
  /master/workerRouter {
    # Uncomment the following two lines to change the calculation to use 10 workers instead of 4:
    #router = round-robin
    #nr-of-instances = 10
  }
}
</pre>
</div>
<p>If you uncomment the two lines, you should see a change in performance,
hopefully for the better (you might want to increase the number of messages in
the code to prolong the time the application runs). It should be noted that
overriding only works if a router type is given, so just uncommenting
<tt class="docutils literal"><span class="pre">nr-of-instances</span></tt> does not work; see <a class="reference internal" href="../java/routing.html#routing-java"><em>Routing (Java)</em></a> for more details.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Make sure that your <tt class="docutils literal"><span class="pre">application.conf</span></tt> is on the class path when you run
the application. If running from inside Maven that should already be the
case, otherwise you need to add the directory containing this file to the
JVM’s <tt class="docutils literal"><span class="pre">-classpath</span></tt> option.</p>
</div>
</div>
<div class="section" id="conclusion">
<h2>Conclusion</h2>
<p>We have learned how to create our first Akka project using Akka&#8217;s actors to speed up a computation-intensive problem by scaling out on multi-core processors (also known as scaling up). We have also learned to compile and run an Akka project using either the tools on the command line or the SBT build system.</p>
<p>If you have a multi-core machine then I encourage you to try out different number of workers (number of working actors) by tweaking the <tt class="docutils literal"><span class="pre">nrOfWorkers</span></tt> variable to for example; 2, 4, 6, 8 etc. to see performance improvement by scaling up.</p>
<p>Happy hakking.</p>
</div>
</div>


          </div>
          <div class="span3"><p class="contents-title">Contents</p>
              <div id="scroller-anchor">
                <div id="scroller">
                  <div id="toc"></div>
                </div>
              </div></div>
        </div>
      </div>
    </div>
  </div>
  <div class="footer">
  <div class="container">
    <ul>
      <li><h5>Akka</h5></li>
      <li><a href="http://akka.io/docs">Documentation</a></li>
      <li><a href="http://akka.io/downloads">Downloads</a></li>
    </ul>
    <ul>
      <li><h5>Contribute</h5></li>
      <li><a href="http://github.com/akka/akka">Source Code</a></li>
      <li><a href="http://groups.google.com/group/akka-user">Mailing List</a></li>      
      <li><a href="http://www.assembla.com/spaces/akka/tickets">Report a Bug</a></li>      
    </ul>
    <ul>
      <li><h5>Company</h5></li>
      <li><a href="http://typesafe.com/products/typesafe-subscription">Commercial Support</a></li>
      <li><a href="http://akka.io/team">Team</a></li>
      <li><a href="mailto:info@typesafe.com">Contact</a></li>
    </ul>
    <ul>
      <li><img src="../_static/watermark.png" align="center"/></li>
    </ul>
  </div>
  <div class="container copyright">
    <p style="float: left;">
      © 2012 <a href="http://typesafe.com/">Typesafe Inc.</a> <span class="license">Akka is Open Source and available under the Apache 2 License.</span>
    </p>
    <p style="float: right; font-size: 12px;">
      Last updated: Apr 13, 2012
    </p>          
  </div>
</div>
<script type="text/javascript">
  $('#toc').toc();
</script>
  

  </body>
</html>